﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace V32VM
{
    public partial class WatchForm : Form, IMemoryUpdatable
    {
        public WatchForm()
        {
            InitializeComponent();
            Watch.OnWatchesChanged += OnWatchesChanged;

            this.OnWatchesChanged();
        }

        private void OnWatchesChanged()
        {
            watchListBox.Items.Clear();
            watchListBox.Items.AddRange(Watch.Watches.ToArray());
        }

        private void addWatchButton_Click(object sender, EventArgs e)
        {
            WatchEditDialog watchEdit = new WatchEditDialog();
            if (watchEdit.ShowDialog() == DialogResult.OK)
            {
                Watch.AddWatch(watchEdit.Watch);
                watchListBox.SelectedIndex = watchListBox.Items.Count - 1;
            }
        }
        private void editWatchButton_Click(object sender, EventArgs e)
        {
            if (watchListBox.SelectedIndex < 0) return;

            WatchEditDialog watchEdit = new WatchEditDialog(
                Watch.Watches[watchListBox.SelectedIndex]);
            if (watchEdit.ShowDialog() == DialogResult.OK)
            {
                Watch.Watches[watchListBox.SelectedIndex] = watchEdit.Watch;
                int selectedIndex = watchListBox.SelectedIndex;
                Watch.WatchesChanged();
                watchListBox.SelectedIndex = selectedIndex;
            }
        }
        private void removeWatchButton_Click(object sender, EventArgs e)
        {
            if (watchListBox.SelectedIndex < 0) return;

            Watch.RemoveWatch((Watch)watchListBox.SelectedItem);
        }

        public void UpdateMemory()
        {
            int selectedIndex = watchListBox.SelectedIndex;
            this.OnWatchesChanged();
            if (selectedIndex < watchListBox.Items.Count)
                watchListBox.SelectedIndex = selectedIndex;
            else watchListBox.SelectedIndex = watchListBox.Items.Count == 0 ?
                -1 : watchListBox.Items.Count - 1;
        }

        private void WatchForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainForm.ViewForms.Remove(this);
        }

        private void WatchForm_KeyDown(object sender, KeyEventArgs e)
        {   
            //if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
            //{
            //    if (watchListBox.Items.Count > 0)
            //        watchListBox.SelectedIndex = 0;
            //}
            //else if (e.KeyCode == Keys.Up)
            //{
            //    if (watchListBox.SelectedIndex < 0 && watchListBox.Items.Count > 0)
            //        watchListBox.SelectedIndex = watchListBox.Items.Count - 1;
            //    else if (watchListBox.SelectedIndex != 0)
            //        watchListBox.SelectedIndex--;
            //}
            //else if (e.KeyCode == Keys.Down && e.Modifiers == Keys.Control)
            //{
            //    if (watchListBox.Items.Count > 0)
            //        watchListBox.SelectedIndex = watchListBox.Items.Count - 1;
            //}
            //else if (e.KeyCode == Keys.Down)
            //{
            //    if (watchListBox.SelectedIndex < 0 && watchListBox.Items.Count > 0)
            //        watchListBox.SelectedIndex = 0;
            //    else if (watchListBox.SelectedIndex != watchListBox.Items.Count - 1)
            //        watchListBox.SelectedIndex++;
            //}

            if (e.KeyCode == Keys.A)
                addWatchButton_Click(null, null);
            else if (e.KeyCode == Keys.E)
                editWatchButton_Click(null, null);
            else if (e.KeyCode == Keys.R)
                removeWatchButton_Click(null, null);
        }
    }
}
